﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;

using System.Collections.Generic;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Web.Configuration;
using Indeco.G24.Web;
using Indeco.G24.Entities;
using Indeco.G24.Services;


namespace Indeco.G24.Website
{
    public partial class CreateSiteMap : System.Web.UI.Page
    {

        private string _siteMapFileName = "SiteMap/Category.sitemap";
        private const String SiteMapNodeName = "siteMapNode";
        protected void Page_Load(object sender, EventArgs e)
        {
            generalSiteMap();
        }
        public void generalSiteMap()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + _siteMapFileName;
            XmlDocument siteMapXml = new XmlDocument();
            siteMapXml.Load(AppDomain.CurrentDomain.BaseDirectory + _siteMapFileName);
            XmlElement rootElement = (XmlElement)siteMapXml.GetElementsByTagName(SiteMapNodeName)[0];
            rootElement.RemoveAll();
            rootElement.SetAttribute("url", "~/Price/Category.aspx");
            rootElement.SetAttribute("title", "Danh mục");
            rootElement.SetAttribute("description", "");
            siteMapXml.Save(path);
            addNode(rootElement, new Guid("00000000-0000-0000-0000-000000000000"));
            siteMapXml.Save(path);
        }
        private void addNode(XmlElement rootElement,Guid IDSanPhamDanhMucCha)
        {
            SanPhamDanhMucService sanPhamDanhMucService = new SanPhamDanhMucService();
            int totalCount = -1;
            TList<SanPhamDanhMuc> listSanPhamDanhMuc = sanPhamDanhMucService.GetPaged("ID_SanPhamDanhMucCha='" + IDSanPhamDanhMucCha.ToString() + "'", "ViTri", 0, 100000, out totalCount);
            foreach (SanPhamDanhMuc sanPhamDanhMuc in listSanPhamDanhMuc)
            {
                XmlElement sanPhamDanhMucNode = AddDynamicChildElement(rootElement, "~/Price/Category.aspx?ID=" + sanPhamDanhMuc.IDSanPhamDanhMuc.ToString(), sanPhamDanhMuc.Ten, "");
                addNode(sanPhamDanhMucNode, sanPhamDanhMuc.IDSanPhamDanhMuc);
            } 
        }
        private static XmlElement AddDynamicChildElement(XmlElement parentElement, String url, String title, String description)
        {
            // Create new element from the parameters
            XmlElement childElement = parentElement.OwnerDocument.CreateElement(SiteMapNodeName);
            childElement.SetAttribute("url", url);
            childElement.SetAttribute("title", title);
            childElement.SetAttribute("description", description);
            // Add it to the parent
            parentElement.AppendChild(childElement);
            return childElement;
        }
    }
    
}